
#define SERIAL_PORT 0
#define BAUDRATE 250000
#define MOTHERBOARD BOARD_RAMPS_14_EFB
#define X_DRIVER_TYPE DRV8825

#define X_DIR_PIN 22
#define X_STEP_PIN 24
#define X_SLP_PIN 26
#define X_RST_PIN 28
#define X_M2_PIN 30
#define X_M1_PIN 32
#define X_M0_PIN 34
/*
  步进电机转速(转/分钟)= 频率 * 60 / ((360/T)* x)
  T = 1.8 固定步进角
  x = 细分倍数
*/
double SPEED_RATE = 5; //若想要改变电机转动的速度 可以通过改变这个延时实现
double STEP_DU = 1.8;
int DEFAULT_STEP_LENGTH = 800;

// #define X_ENABLE_PIN 36

/*
    M0  M1  M2   细分
    L   L   L    全步 1.8度
    H   L   L    半步
    L   H   L    1/4步
    H   H   L    1/8步
    L   L   H    1/16步
    H   L   H    1/32步
    L   H   H    1/32步
    H   H   H    1/32步
*/
void speardStep(int phase) {
  switch (phase) {
    case 1:
      full();
      break;
    case 2:
      half();
      break;
    case 3:
      one_Four();
      break;
    case 4:
      one_Eight();
      break;
    case 5:
      one_sixten();
      break;
    case 6:
      one_thridtwo();
      break;
    default:
      Serial.print("Invalid state!");
      break;
  }
}
/*
    M0  M1  M2   细分
    L   L   L    全步 1.8度
*/
void full() {
  digitalWrite(X_M2_PIN, LOW);
  digitalWrite(X_M1_PIN, LOW);
  digitalWrite(X_M0_PIN, LOW);
  // 频率 * 60 / ((360/T)* x)
  SPEED_RATE = 1 * 60 / ((360 / STEP_DU) * (1 / 1));
}
/*
    M0  M1  M2   细分
    H   L   L    半步
*/
void half() {
  digitalWrite(X_M2_PIN, LOW);
  digitalWrite(X_M1_PIN, LOW);
  digitalWrite(X_M0_PIN, HIGH);
  SPEED_RATE = 1 * 60 / ((360 / STEP_DU) * (1 / 2));
}
/*
    M0  M1  M2   细分
    L   H   L    1/4步
*/
void one_Four() {
  digitalWrite(X_M2_PIN, LOW);
  digitalWrite(X_M1_PIN, HIGH);
  digitalWrite(X_M0_PIN, LOW);
  SPEED_RATE = 1 * 60 / ((360 / STEP_DU) * (1 / 4));
}
/*
    M0  M1  M2   细分
    H   H   L    1/8步
*/
void one_Eight() {
  digitalWrite(X_M2_PIN, LOW);
  digitalWrite(X_M1_PIN, HIGH);
  digitalWrite(X_M0_PIN, HIGH);
  SPEED_RATE = 1 * 60 / ((360 / STEP_DU) * (1 / 8));
}
/*
    M0  M1  M2   细分
    L   L   H    1/16步
*/
void one_sixten() {
  digitalWrite(X_M2_PIN, HIGH);
  digitalWrite(X_M1_PIN, LOW);
  digitalWrite(X_M0_PIN, LOW);
  SPEED_RATE = 1 * 60 / ((360 / STEP_DU) * (1 / 16));
}
/*
    M0  M1  M2   细分
    H   L   H    1/32步
    L   H   H    1/32步
    H   H   H    1/32步
*/
void one_thridtwo() {
  digitalWrite(X_M2_PIN, HIGH);
  digitalWrite(X_M1_PIN, LOW);
  digitalWrite(X_M0_PIN, HIGH);
  SPEED_RATE = 1000 * 60 / ((360 / STEP_DU) * (1 / 32));
}
void smmsteperTest() {
  //用于计算步数
  int i = 0;
  /*全步正转360度*/
  digitalWrite(X_DIR_PIN, HIGH);  //正转
  while (i < DEFAULT_STEP_LENGTH) {
    digitalWrite(X_RST_PIN, HIGH);
    //细分
    speardStep(4);
    digitalWrite(X_STEP_PIN, HIGH);
    delayMicroseconds(300);
    digitalWrite(X_STEP_PIN, LOW);
    delay(SPEED_RATE);
    i++;
    Serial.println(i);
  }

  /*全步反转360度*/
  digitalWrite(X_DIR_PIN, LOW);  //反转
  while (i > 0) {
    digitalWrite(X_RST_PIN, HIGH);
    //细分
    speardStep(4);
    digitalWrite(X_STEP_PIN, HIGH);
    delayMicroseconds(300);
    digitalWrite(X_STEP_PIN, LOW);
    delay(SPEED_RATE);
    i--;
    Serial.println(i);
  }
}

void smmsteperRun(int enpin, int trunpin, int pwpin, int step, int trun) {
  pinMode(enpin, OUTPUT);
  digitalWrite(enpin, LOW);
  pinMode(trunpin, OUTPUT);
  digitalWrite(trunpin, trun);
  for (int i = (1); i <= (step); i = i + (1)) {
    pinMode(pwpin, OUTPUT);
    digitalWrite(pwpin, HIGH);
    delay(1);
    pinMode(pwpin, OUTPUT);
    digitalWrite(pwpin, LOW);
    delay(1);
  }
  pinMode(enpin, OUTPUT);
  digitalWrite(enpin, HIGH);
}

void setup() {
  pinMode(X_DIR_PIN, OUTPUT);
  pinMode(X_DIR_PIN, OUTPUT);
  pinMode(X_STEP_PIN, OUTPUT);
  pinMode(X_SLP_PIN, OUTPUT);
  pinMode(X_RST_PIN, OUTPUT);
  pinMode(X_M2_PIN, OUTPUT);
  pinMode(X_M1_PIN, OUTPUT);
  pinMode(X_M0_PIN, OUTPUT);
  digitalWrite(X_SLP_PIN, HIGH);  //唤醒驱动
  Serial.begin(9600);
}

void loop() {
  smmsteperTest();
}